<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .container {
      width: 800px;
      margin: 50px;
    }

    #box {
      width: 500px;
      height: 500px;
      border: solid 1px rgb(255, 0, 179);
      position: relative;
      margin-top: 10px;
    }

    #move {
      width: 100px;
      height: 100px;
      background-color: blueviolet;
      position: absolute;
      border-radius: 50%;
    }
  </style>
</head>

<body>
  <div class="container">
    请输入速度：<input type="text" id="speed">
    <button id="start-button" type="button">开始</button>
  </div>
  <div id="box">
    <div id="move"></div>
  </div>
</body>
<script>
  var startButton = document.getElementById('start-button')
  var move = document.getElementById('move')
  var box = document.getElementById('box')
  var Xmax = box.offsetWidth - move.offsetWidth
  var Ymax = box.offsetHeight - move.offsetHeight
  var x = move.offsetLeft
  var y = move.offsetTop
  console.log(Xmax, Ymax, x, y)
  var XmyId;
  var YmyId;
  // console.log(myId)
  // startButton.addEventListener('click', animationMoveX(move, Xmax))
  // startButton.addEventListener('click', animationMoveY(move, Ymax))
  // startButton.addEventListener('click', animationMoveX(move, x))
  // startButton.addEventListener('click', animationMoveY(move, y))
  startButton.onclick = function (e) {
    var speed = document.getElementById('speed').value
    var r = animationMoveX(move, Xmax, speed)
    var d = window.setTimeout(function () {
      console.log('向下移动')
      animationMoveY(move, Ymax, speed)
    }, Xmax / speed * 30)
    var l = window.setTimeout(function () {
      console.log('向左移动')
      animationMoveX(move, x, speed)
    }, Xmax / speed * 30 + Ymax / speed * 30)
    var u = window.setTimeout(function () {
      console.log('向上移动')
      animationMoveY(move, y, speed)
    }, Xmax / speed * 30 * 2 + Ymax / speed * 30)

    /*  animationMoveX(move, Xmax, speed)
     console.log('向右移动')
     if (move.offsetLeft == Xmax) {
       console.log('向下移动')
       animationMoveX(move, Ymax, speed)
       if (move.offsetTop == Ymax) {
         console.log('向左移动')
         animationMoveX(move, x, speed)
         if (move.offsetLeft == 0) {
           console.log('向上移动')
           animationMoveY(move, y, speed)
         }
       }
     } */
    //console.log('右移');
    // console.log('向下移动')
    //animationMoveX(move, Ymax, speed)
    // console.log('向左移动')
    // animationMoveX(move, x)
    // console.log('向上移动')
    // animationMoveY(move, y)
  }
  function animationMoveX(ele, target, speed) {
    clearInterval(XmyId)
    XmyId = setInterval(function () {
      var x = ele.offsetLeft
      if (x == target) {
        clearInterval(XmyId)
      } else if (x > target) {
        x -= parseInt(speed)
        console.log('x -= 15=' + x)
        x = x < target ? target : x
        console.log('x =' + x)
        ele.style.left = x + 'px'
      } else if (x < target) {
        x += parseInt(speed)
        console.log('x += 15=' + x)
        x = x > target ? target : x
        ele.style.left = x + 'px'
      }
    }, 30)

  }
  function animationMoveY(ele, target, speed) {
    clearInterval(YmyId)
    YmyId = setInterval(function () {
      var y = ele.offsetTop
      if (y == target) {
        clearInterval(YmyId)
      } else if (y > target) {
        console.log('target=' + target)
        y -= parseInt(speed)
        console.log('y - speed=' + y)
        y = y < target ? target : y
        console.log('y 比较之后=' + y)
        ele.style.top = y + 'px'
      } else if (y < target) {
        y += parseInt(speed)
        console.log('y += 15=' + y)
        y = y > target ? target : y
        ele.style.top = y + 'px'
      }
    }, 30)
  }
</script>

</html>